/*
 * FileName:       main.h
 * Author:         Kingsea  Version: v1.0  Date: 2019-5-17
 * Description:    
 * History:        
 *     <author>   	<version>    	<time>    	<desc>
 *     Kingsea   	v1.0    	2019-5-17    
 */

#include <stdint.h>
#include <string.h>
#include <stdarg.h>
#include "nordic_common.h"
#include "nrf.h"
#include "app_error.h"
#include "ble.h"
#include "ble_hci.h"
#include "ble_srv_common.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "ble_bas.h"
#include "ble_hrs.h"
#include "ble_dis.h"
#include "ble_nus.h"
#include "ble_conn_params.h"
#include "sensorsim.h"
#include "nrf_sdh.h"
#include "nrf_sdh_soc.h"
#include "nrf_sdh_ble.h"
#include "nrf_sdh_freertos.h"
#include "app_timer.h"
#include "app_uart.h"
#include "peer_manager.h"
#include "bsp_btn_ble.h"
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "semphr.h"
#include "fds.h"
#include "ble_conn_state.h"
#include "nrf_drv_clock.h"
#include "nrf_ble_gatt.h"

#include "nrf_dfu_svci.h"
#include "nrf_svci_async_function.h"
#include "nrf_svci_async_handler.h"
#include "nrf_pwr_mgmt.h"
#include "ble_dfu.h"

#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_drv_wdt.h"
#include "nrf_drv_pwm.h"

#ifndef MAIN_H__
#define MAIN_H__
#ifdef __cplusplus
extern "C"{
#endif 

// software main version
#define SW_MAIN_VERSION_S               	"17" // Vx.x string

#define BLE_GAP_PHY_2MBPS_EN 				(0)
#define BLE_PIN_CONTROL_EN					(1)
#define HW_UART_BAUDRATE_SET				(NRF_UART_BAUDRATE_115200) // (NRF_UART_BAUDRATE_460800) // 
#define BLE_GATT_MAX_MTU_SIZE 				(83) // (NRF_SDH_BLE_GATT_MAX_MTU_SIZE) // 
#define USERCMD_QUEUE_LENGTH 				(8)

#define DEVICE_NAME                         "extDevices"                            /**< Name of device. Will be included in the advertising data. */
#define MANUFACTURER_NAME                   "NordicSemiconductor"                   /**< Manufacturer. Will be passed to Device Information Service. */
#define NUS_SERVICE_UUID_TYPE               BLE_UUID_TYPE_VENDOR_BEGIN              /**< UUID type for the Nordic UART Service (vendor specific). */

#define APP_BLE_OBSERVER_PRIO               3                                       /**< Application's BLE observer priority. You shouldn't need to modify this value. */
#define APP_BLE_CONN_CFG_TAG                1                                       /**< A tag identifying the SoftDevice BLE configuration. */

#define APP_ADV_INTERVAL                    MSEC_TO_UNITS(40, UNIT_0_625_MS)        /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
#define APP_ADV_TIMEOUT_IN_SECONDS          30                                      /**< The advertising timeout (in units of seconds). */

#define BATTERY_LEVEL_MEAS_INTERVAL         2000                                    /**< Battery level measurement interval (ms). */
#define MIN_BATTERY_LEVEL                   81                                      /**< Minimum simulated battery level. */
#define MAX_BATTERY_LEVEL                   100                                     /**< Maximum simulated battery level. */
#define BATTERY_LEVEL_INCREMENT             1                                       /**< Increment between each simulated battery level measurement. */

#define MIN_CONN_INTERVAL                   MSEC_TO_UNITS( 7.5, UNIT_1_25_MS)       /**< Minimum acceptable connection interval (15 milliseconds). */ //BLE_GAP_CP_MIN_CONN_INTVL_MIN
#define MAX_CONN_INTERVAL                   MSEC_TO_UNITS( 75, UNIT_1_25_MS)        /**< Maximum acceptable connection interval (75 milliseconds). */ //BLE_GAP_CP_MAX_CONN_INTVL_MAX
#define SLAVE_LATENCY                       0                                       /**< Slave latency. */
#define CONN_SUP_TIMEOUT                    MSEC_TO_UNITS(4000, UNIT_10_MS)         /**< Connection supervisory time-out (4 seconds). */

#define FIRST_CONN_PARAMS_UPDATE_DELAY      APP_TIMER_TICKS(5000)                   /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
#define NEXT_CONN_PARAMS_UPDATE_DELAY       APP_TIMER_TICKS(30000)                  /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
#define MAX_CONN_PARAMS_UPDATE_COUNT        3                                       /**< Number of attempts before giving up the connection parameter negotiation. */

#define SEC_PARAM_BOND                      1                                       /**< Perform bonding. */
#define SEC_PARAM_MITM                      0                                       /**< Man In The Middle protection not required. */
#define SEC_PARAM_LESC                      0                                       /**< LE Secure Connections not enabled. */
#define SEC_PARAM_KEYPRESS                  0                                       /**< Keypress notifications not enabled. */
#define SEC_PARAM_IO_CAPABILITIES           BLE_GAP_IO_CAPS_NONE                    /**< No I/O capabilities. */
#define SEC_PARAM_OOB                       0                                       /**< Out Of Band data not available. */
#define SEC_PARAM_MIN_KEY_SIZE              7                                       /**< Minimum encryption key size. */
#define SEC_PARAM_MAX_KEY_SIZE              16                                      /**< Maximum encryption key size. */

#define DEAD_BEEF                           0xDEADBEEF                              /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */

#define OSTIMER_WAIT_FOR_QUEUE              2                                       /**< Number of ticks to wait for the timer queue to be ready */

#define APP_FEATURE_NOT_SUPPORTED           BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2    /**< Reply when unsupported features are requested. */

#define BLE_APPEARANCE_DEVICEID 			(BLE_APPEARANCE_UNKNOWN+20000)

#define UART_TX_BUF_SIZE                256                                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE                128                                         /**< UART RX buffer size. */


#define appuart_printf(...) 	printf(__VA_ARGS__)
//#define appuart_printf(...)

//////////////////////////////////////////////////////////////////////////
uint32_t uart_printf(const void *fmt, ...);
ble_nus_t * get_nus_object(void);
uint32_t nus_printf(const void *fmt, ...);
uint16_t getCurrentConnHandle(void);
ble_advertising_t * getAdvertisingObject(void);
uint32_t main_bsp_indication_set(bsp_indication_t indicate);
bsp_indication_t main_bsp_indication_get(void);

#ifdef __cplusplus
}
#endif 
#endif /* MAIN_H__ */ 

